﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Drawing;
using Antu.Business.DmpExcel;

namespace WebSys.UI.Control.ZhengShou
{
    public class HuoBiHuaListControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        public CustomGridView CustomGridList1;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            Button btnSearch = (Button)FindControl("btnSearch");
            btnSearch.Click += new EventHandler(btnSearch_Click);
            Button Button2 = (Button)FindControl("Button2");
            Button btnRY = (Button)FindControl("btnRY");
            Button2.Click += new EventHandler(Button2_Click);
            btnRY.Click += new EventHandler(btnRY_Click);
            Button Button3 = (Button)FindControl("Button3");
            Button3.Click += new EventHandler(Button3_Click);

            CustomGridList1.OnLoadData += new EventHandler(CustomGridList1_OnLoadData);
            CustomGridList1.RowCreated += new GridViewRowEventHandler(CustomGridList1_RowCreated);
            CustomGridList1.RowDataBound += new GridViewRowEventHandler(CustomGridList1_RowDataBound);
            
            if (!base.Page.IsPostBack) {                
                Bind();
            }
            
        }
        //导出Excel
        protected void Button3_Click(object sender, EventArgs e)
        {
            string strpid = base.Page.Request["PID"];
            string sql = @"select *
  from (select 
               (zsjdh ||textbox1) 协议号,
               z.产权人,
               z.承租人,
               z.证件号,
               z.房屋坐落,
               z.丘权号,
               z.房屋权证编号,
               h.id,
               h.pnid,
               Nvl(Replace(h.Textbox19, '\', ''), 0) 补偿款,
       To_Number(Nvl(Replace(h.Textbox24, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox25, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox26, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox27, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox28, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox29, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox30, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox31, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox32, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox125, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox126, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox34, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox33, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox122, '\', ''), 0)) 装修及其它补偿补助款,
               h.personid,h.消除异常,
               h.textbox1,
               h.TextBox73 房屋建筑面积
          from zs_hbhxy h, ZS_ZSPERSON z
         where h.personid = z.id
           and h.PNID = '" + strpid + "' order by z.addtime desc) zh where 1=1 " + GetSqlWhere() + " Order By zh.textbox1 Desc";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            ExcelStruct excStruct = new ExcelStruct();
            excStruct.DataTableTemp = dt;
            excStruct.PageSource = this.Page;
            excStruct.Flag = "0";
            excStruct.TableName = "货币化补偿协议表";
            excStruct.HiddenField = "ID,PNID,PERSONID,消除异常,TEXTBOX1";
            DmpExcel dmpExcel = new DmpExcel();
            dmpExcel.ExpExcel(excStruct);
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Bind();
        }
        protected void Bind()
        {
          
            string strpid = base.Page.Request["PID"];
          
            DataTable dt =  GetHBHXY(strpid, GetSqlWhere());
            CustomGridList1.DataSource = dt;
            CustomGridList1.RecordCount = dt.Rows.Count;
            CustomGridList1.DataKeyNames = new string[] { "ID" };
            CustomGridList1.DataBind();

           
            EluZSProvider.YJCS yjcs = _EluZSDataProvider.GetYJCS();
            Label Label1 = (Label)FindControl("Label1");
            Label1.Text = yjcs.补偿装修费用预警值;
           
        }

        public DataTable GetHBHXY(string pid, string strwhere)
        {
            DropDownList 排序 = (DropDownList)FindControl("排序");
            string sql = string.Format(@"select *
  from (select 
               z.产权人,
               z.承租人,
               z.证件号,
               z.房屋坐落,
               z.丘权号,
               z.房屋权证编号,
               h.id,
               h.pnid,
               h.personid,h.消除异常,(zsjdh ||textbox1) 协议号,
               h.textbox1,
               h.备案时间,
               Nvl(Replace(h.Textbox73, '\', ''), 0) 房屋建筑面积,
               Nvl(Replace(h.Textbox19, '\', ''), 0) 补偿款,
       To_Number(Nvl(Replace(h.Textbox24, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox25, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox26, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox27, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox28, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox29, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox30, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox31, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox32, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox125, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox126, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox34, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox33, '\', ''), 0)) +
            To_Number(Nvl(Replace(h.Textbox122, '\', ''), 0)) 装修及其它补偿补助款,
To_Number(Nvl(Replace(h.TextBox48, '\', ''), 0)) +
               To_Number(Nvl(Replace(h.TextBox51, '\', ''), 0)) 营业补偿补助款,
            h.审核状态,
            h.提交备案,
            h.市备案状态,
            z.Addtime
          from zs_hbhxy h, ZS_ZSPERSON z
         where h.personid = z.id
           and h.PNID = '{0}' order by z.addtime desc) zh where 1=1 ", pid);
            if (!string.IsNullOrEmpty(strwhere)) { sql += strwhere; }
            if (排序.SelectedValue == "协议号")
            {
                sql += " Order By zh.textbox1 Desc";
            }
            if (排序.SelectedValue == "备案时间")
            {
                sql += " Order By zh.审核状态,zh.提交备案,zh.市备案状态,zh.备案时间 Desc";
            }

            DataTable dt = _EluZSDataProvider.GetTable(sql);
            return dt;
        }

        protected void CustomGridList1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            //TableCell cell = new TableCell();

            //if (e.Row.RowType == DataControlRowType.Header)
            //{
            //    TableHeaderCell cell1 = new TableHeaderCell();
            //    cell1.Text = "选择";
            //    e.Row.Cells.AddAt(0, cell1);
            //}

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView rows = e.Row.DataItem as DataRowView;

                HtmlInputRadioButton rb = (HtmlInputRadioButton)e.Row.FindControl("rb");
                

                string tt = base.Page.Request["tt"];
                if (!string.IsNullOrEmpty(tt) && tt != "")
                {
                    string[] tenm = tt.Split(',');
                    if (tenm.Length > 0)
                    {
                        for (int i = 0; i < tenm.Length; i++)
                        {
                            if (rows["ID"].ToString() == tenm[i])
                            {
                                rb.Checked = true;
                            }
                        }
                    }
                }
                if (rows != null)
                {
                    e.Row.Attributes.Add("valueid", rows["ID"].ToString());

                    rb.Attributes.Add("onclick", "setValue(this);");
                    rb.Attributes.Add("valueid", rows["ID"].ToString());
                }
                else
                {
                    e.Row.Attributes.Add("valueid", "");
                    rb.ID = "";
                    rb.Attributes.Add("onclick", "setValue(this);");
                }
                //cell.Controls.Add(rb);
                //cell.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                //e.Row.Cells.AddAt(0, cell);
            }
        }
        protected void CustomGridList1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView rowview = (DataRowView)e.Row.DataItem;
                string strHYC = rowview["消除异常"].ToString();
                string pnid = base.Page.Request["PID"];
                string PERSONID = ((HiddenField)e.Row.FindControl("HiddenField1")).Value;
                string gettemp1 = _EluZSDataProvider.GetPGBCK(PERSONID, pnid, "0");
                string gettt1 = string.Empty;
                string gettt2 = string.Empty;
                string gettemp2 = _EluZSDataProvider.GetPGBCK(PERSONID, pnid, "1");
                string gettt3 = string.Empty;
                string gettt4 = string.Empty;
                if (gettemp1.Length > 0)
                {
                    String[] arrtemp1 = gettemp1.Split(',');
                    if (arrtemp1.Length > 0)
                    {
                        gettt1 = arrtemp1[0].ToString();//补偿
                        gettt2 = arrtemp1[1].ToString();//装修
                    }
                }
                if (gettemp2.Length > 0)
                {
                    String[] arrtemp2 = gettemp2.Split(',');
                    if (arrtemp2.Length > 0)
                    {
                        gettt3 = arrtemp2[0].ToString();//补偿
                        gettt4 = arrtemp2[1].ToString();//装修
                    }
                }
                if (string.IsNullOrEmpty(gettt1) || gettt1 == "&nbsp;") { gettt1 = "0"; }
                if (string.IsNullOrEmpty(gettt2) || gettt2 == "&nbsp;") { gettt2 = "0"; }
                if (string.IsNullOrEmpty(gettt3) || gettt3 == "&nbsp;") { gettt3 = "0"; }
                if (string.IsNullOrEmpty(gettt4) || gettt4 == "&nbsp;") { gettt4 = "0"; }
              //  e.Row.Cells[9].Text = gettt3;
              //  e.Row.Cells[10].Text = gettt4;
                string tt1 = e.Row.Cells[10].Text;
                string tt2 = e.Row.Cells[11].Text;
                EluZSProvider.YJCS yjcs = _EluZSDataProvider.GetYJCS();
                string strYJCS = yjcs.补偿装修费用预警值;
                if (string.IsNullOrEmpty(strYJCS) || strYJCS == "&nbsp;")
                {
                    strYJCS = "0";
                }
                double bfb = Convert.ToDouble(strYJCS) / 100;
                string strBZY = yjcs.补偿装修费用已超过预警值;
                string strBZJ = yjcs.补偿价款已超过房屋均价;
                if (string.IsNullOrEmpty(tt1) || tt1 == "&nbsp;") { tt1 = "0"; }
                if (string.IsNullOrEmpty(tt2) || tt2 == "&nbsp;") { tt2 = "0"; }
                double temp = Convert.ToDouble(tt1) + Convert.ToDouble(tt2);
                double pgyjc = Convert.ToDouble(gettt1) + Convert.ToDouble(gettt2);
                if (strBZY == "1")
                {
                    double tt = temp - Convert.ToDouble(pgyjc) * bfb;//130%
                    if (tt > 0)
                    {
                        if (string.IsNullOrEmpty(strHYC))
                        {
                            for (int i = 0; i < e.Row.Cells.Count; i++)
                            {
                                e.Row.Cells[i].BackColor = Color.FromName("#FE6501");
                            }
                        }
                        else
                        {
                            e.Row.Cells[4].Text = "<font color='#FE6501'>★</font>" + e.Row.Cells[4].Text;
                        }
                    }
                    else
                    {
                        double ttt1 = temp - Convert.ToDouble(pgyjc);
                        if (ttt1 > 0)
                        {
                            if (string.IsNullOrEmpty(strHYC))
                            {
                                for (int i = 0; i < e.Row.Cells.Count; i++)
                                {
                                    e.Row.Cells[i].BackColor = Color.FromName("#a8d54a");
                                }
                            }
                            else
                            {
                                e.Row.Cells[4].Text = "<font color='#a8d54a'>★</font>" + e.Row.Cells[4].Text;
                            }
                        }
                    }

                }
                if (strBZJ == "1")//此区域房屋均价为多少未知
                {
                    string pid = base.Page.Request["PID"];
                    if (!string.IsNullOrEmpty(pid))
                    {
                        string fwjj = _EluZSDataProvider.GetQYFWJJ(PERSONID, pid);
                        if (string.IsNullOrEmpty(fwjj) || fwjj == "&nbsp;")
                        {
                            fwjj = "0";
                        }
                        int qyjj = Convert.ToInt32(fwjj);//万元-元
                        double t1 = temp - Convert.ToDouble(qyjj) * bfb;//130%
                        if (t1 > 0)
                        {
                            if (string.IsNullOrEmpty(strHYC))
                            {
                                for (int i = 0; i < e.Row.Cells.Count; i++)
                                {
                                    e.Row.Cells[i].BackColor = Color.FromName("Red");
                                }
                            }
                            else
                            {
                                e.Row.Cells[4].Text = "<font color='red'>◆</font>" + e.Row.Cells[4].Text;
                            }
                        }
                        else
                        {
                            double ttt1 = temp - temp - Convert.ToDouble(qyjj);
                            if (ttt1 > 0)
                            {
                                if (string.IsNullOrEmpty(strHYC))
                                {
                                    for (int i = 0; i < e.Row.Cells.Count; i++)
                                    {
                                        e.Row.Cells[i].BackColor = Color.FromName("#de7474");
                                    }
                                }
                                else
                                {
                                    e.Row.Cells[4].Text = "<font color='#de7474'>◆</font>" + e.Row.Cells[4].Text;
                                }
                            }
                        }
                    }
                }
                string strid = rowview["ID"].ToString();
                //提交状态
                string strTJZT = _EluZSDataProvider.GetString("select 审核状态 from zs_hbhxy where id='" + strid + "'");
                if (strTJZT == "1")
                {
                    for (int i = 0; i < e.Row.Cells.Count; i++)
                    {
                        e.Row.Cells[i].BackColor = Color.FromName("Yellow");
                    }
                }
                //审核状态
                string strSHZT = _EluZSDataProvider.GetString("select 提交备案 from zs_hbhxy where id='" + strid + "'");
                if (strSHZT=="1")
                {
                    for (int i = 0; i < e.Row.Cells.Count; i++)
                    {
                        e.Row.Cells[i].BackColor = Color.FromName("Green");
                    }
                }
                //审核状态
                string strSBZZT = _EluZSDataProvider.GetString("select 市备案状态 from zs_hbhxy where id='" + strid + "'");
                if (strSBZZT == "1")
                {
                    //for (int i = 0; i < e.Row.Cells.Count; i++)
                    //{
                        
                    //}
                    e.Row.Cells[4].BackColor = Color.FromName("#9ED6F6");
                }
            }
        }
        protected void CustomGridList1_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }
        protected string GetSqlWhere()
        {
            string sqlWhere = string.Empty;
            TextBox 产权人 = (TextBox)FindControl("产权人");
            TextBox 房屋坐落 = (TextBox)FindControl("房屋坐落");
            HiddenField hfdQqh = (HiddenField)FindControl("hfdQqh");
            if (!string.IsNullOrEmpty(产权人.Text.Trim()))
            {
                sqlWhere += string.Format(" and (zh.产权人 like '%{0}%' or zh.承租人 like '%{0}%')", 产权人.Text.Trim());
            }
            if (!string.IsNullOrEmpty(房屋坐落.Text.Trim()))
            {
                sqlWhere += string.Format(" and zh.房屋坐落 like '%{0}%'", 房屋坐落.Text.Trim());
            }
            if (!string.IsNullOrEmpty(hfdQqh.Value))
            {
                sqlWhere += string.Format(" and zh.personid in ('{0}')", hfdQqh.Value.Replace("|", "','"));
            }

            return sqlWhere;
        }
        /// <summary>
        /// 新增人员关联按钮事件
        /// </summary>
        protected void btnRY_Click(object sender, EventArgs e)
        {
            string pid = base.Page.Request["PID"].ToString();
            string values = "PerSelect.aspx?PID=" + pid + "";
            base.Page.ClientScript.RegisterStartupScript(this.GetType(), "selecthouse", string.Format("<script>SelectHouse('{0}');</script>", values));
            Bind();
        }
        /// <summary>
        /// 消除异常按钮事件
        /// </summary>
        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox project_id = (TextBox)FindControl("project_id");
            string ttid = project_id.Text;
            if (!string.IsNullOrEmpty(ttid))
            {
                string strScript = "GetExceptionID.aspx?id=" + ttid;
                base.Page.ClientScript.RegisterStartupScript(this.GetType(), "XCException", string.Format("<script>XCException('{0}');window.location.href=location.href;</script>", strScript));
            }
            else
            {
                base.Page.Response.Write("<script>alert('请选择人员消除异常!');window.location.href=location.href;</script>");
            }
        }
    }
}


